package com.scrumtrek.trainings.tdd;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import static junit.framework.Assert.assertEquals;

//import org.testng.annotations.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
//import static org.testng.AssertJUnit.assertEquals;

public class JUnitBookStoreServiceTestMock {
    private Store store;
    private BookStoreService bookStoreService;

    @Before
    public void HashSetUp() {
        store = mock(Store.class);
        bookStoreService = new BookStoreService(store);
        System.out.println("Test HashSetUp");
    }

    @Test
    public void test1Empty(){
        //Act
        bookStoreService.getTotalBooksAmount();
        //Assert
        verify(store).getCount();
    }

    @Test
    public void test2AddBook(){
        //Act
        Book book = new Book("А. С. Пушкин", "");
        bookStoreService.addBook(book);
        //Assert
        verify(store).addItem(book);
    }

    @Test
     public void test3GetAuthorsOne(){
        //Act
        Book book = new Book("А. С. Пушкин", "");
        bookStoreService.addBook(book);
        Set<String> result = bookStoreService.getAuthors();
        HashSet<String> origin = new HashSet<String>();
        origin.add(book.getAuthor());
        //Assert
        assertEquals("No one author", origin, result);
    }

    @Test
    public void test3GetAuthorsMoreThanOne(){
        //Act
        Book book1 = new Book("А. С. Пушкин", ""),
             book2 = new Book("М. Ю. Лермонтов", "");
        bookStoreService.addBook(book1);
        bookStoreService.addBook(book2);
        Set<String> result = bookStoreService.getAuthors();
        HashSet<String> origin = new HashSet<String>();
        origin.add(book1.getAuthor());
        origin.add(book2.getAuthor());
        //Assert
        assertEquals("No more authors ;-)", origin, result);
    }

    @Test
    public void test4GetAuthorsSame(){
        //Act
        Book book1 = new Book("А. С. Пушкин", ""),
                book2 = new Book("А. С. Пушкин", "");
        bookStoreService.addBook(book1);
        bookStoreService.addBook(book2);
        Set<String> result = bookStoreService.getAuthors();
        HashSet<String> origin = new HashSet<String>();
        origin.add("А. С. Пушкин");
        //Assert
        assertEquals("No unique authors", origin, result);
    }

    @Test
    public void test5GetTitlesOne(){
        //Act
        Book book = new Book("", "Капитанская дочка");
        bookStoreService.addBook(book);
        Set<String> result = bookStoreService.getTitles();
        HashSet<String> origin = new HashSet<String>();
        origin.add(book.getTitle());
        //Assert
        assertEquals("No one title", origin, result);
    }

    @Test
    public void test5GetTitlesMoreThanOne(){
        //Act
        Book book1 = new Book("", "Капитанская дочка"),
                book2 = new Book("", "Мцыри");
        bookStoreService.addBook(book1);
        bookStoreService.addBook(book2);
        Set<String> result = bookStoreService.getTitles();
        HashSet<String> origin = new HashSet<String>();
        origin.add(book1.getTitle());
        origin.add(book2.getTitle());
        //Assert
        assertEquals("No more titles ;-)", origin, result);
    }

    @Test
    public void test6GetTitlesSame(){
        //Act
        Book book1 = new Book("", "Капитанская дочка"),
                book2 = new Book("", "Капитанская дочка");
        bookStoreService.addBook(book1);
        bookStoreService.addBook(book2);
        Set<String> result = bookStoreService.getTitles();
        HashSet<String> origin = new HashSet<String>();
        origin.add("Капитанская дочка");
        //Assert
        assertEquals("No unique titles", origin, result);
    }

    @Test
    public void test7FindByAuthor(){
        //Act
        Book book = new Book("А. С. Пушкин", "Капитанская дочка");
        bookStoreService.addBook(book);
        ArrayList<Book> result = bookStoreService.findByAuthor("А. С. Пушкин"),
                        origin = new ArrayList<Book>();
        origin.add(book);
        //Assert
        assertEquals("No find book by author", origin, result);
    }

    @Test
    public void test8FindByTitle(){
        //Act
        Book book = new Book("А. С. Пушкин", "Капитанская дочка");
        bookStoreService.addBook(book);
        ArrayList<Book> result = bookStoreService.findByTitle("Капитанская дочка"),
                origin = new ArrayList<Book>();
        origin.add(book);
        //Assert
        assertEquals("No find book by title", origin, result);
    }

    @Test
    public void test9FindByAuthorAll(){
        //Act
        Book book1 = new Book("А. С. Пушкин", "Капитанская дочка"),
             book2 = new Book("А. С. Пушкин", "Евгений Онегин"),
             book3 = new Book("М. Ю. Лермонтов", "Мцыри");
        bookStoreService.addBook(book1);
        bookStoreService.addBook(book2);
        bookStoreService.addBook(book3);
        ArrayList<Book> result = bookStoreService.findByAuthor("А. С. Пушкин"),
                origin = new ArrayList<Book>();
        origin.add(book1);
        origin.add(book2);
        //Assert
        assertEquals("No find book by title", origin, result);
    }

    @Test
     public void test10PublisherSet(){
        //Act
        Book book = new Book("А. С. Пушкин", "Капитанская дочка", "АПресс");
        bookStoreService.addBook(book);
        Set<String> result = bookStoreService.getTitles(),
                origin = new HashSet<String>();
        origin.add("Капитанская дочка [АПресс]");
        //Assert
        assertEquals("No publisher", origin, result);
    }

    @Test
    public void test11YearSet(){
        //Act
        Book book = new Book("А. С. Пушкин", "Капитанская дочка", null, "2001");
        bookStoreService.addBook(book);
        Set<String> result = bookStoreService.getTitles(),
                origin = new HashSet<String>();
        origin.add("Капитанская дочка [2001]");
        //Assert
        assertEquals("No year", origin, result);
    }

    @Test
    public void test11PublisherSetYearSet(){
        //Act
        Book book = new Book("А. С. Пушкин", "Капитанская дочка", "АПресс", "2001");
        bookStoreService.addBook(book);
        Set<String> result = bookStoreService.getTitles(),
                origin = new HashSet<String>();
        origin.add("Капитанская дочка [АПресс] [2001]");
        //Assert
        assertEquals("No publisher and/or year", origin, result);
    }

    @Test
    public void test12BookStoreServiceSellBook(){
        //Act
        Book book = new Book("А. С. Пушкин", "Евгений Онегин");
        bookStoreService.addBook(book);
        bookStoreService.sellBook(book);
        //Assert
        verify(store).sellItem(book);
        assertEquals("Book sell, but exist in authors", bookStoreService.getAuthors().contains("А. С. Пушкин"), false);
        assertEquals("Book sell, but exist in titles", bookStoreService.getTitles().contains("Евгений Онегин"), false);
    }

    @After
    public void tearDown(){
        System.out.println("Test tearDown");
    }
}
